using System;
using System.Collections.Generic;
using System.Text;
using MoEngine;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using MoGameLib;

namespace MoGame
{
    public class WindowTileSites : Window
    {
        public WindowTileSites(WindowBase parent)
            : base(parent)
        {
            //m_SpriteBatch = new SpriteBatch(Core.GlobalDevice);
            frame = Core.Content.Load<Texture2D>("Images/GUI/SiteIconFrame");
            icon = Core.Content.Load<Texture2D>("Images/Site/Icons/City");
            separation = Core.Content.Load<Texture2D>("Images/GUI/SeparationLine");
            font = Core.Content.Load<SpriteFont>("Fonts/Arial");

            m_SiteItemsLayer = 0;
        }

        //protected SpriteBatch m_SpriteBatch;

        protected Texture2D frame;
        protected Texture2D separation;
        protected Texture2D icon;
        protected SpriteFont font;

        protected override void DrawWindow(GameTime time)
        {
            base.DrawWindow(time);

            m_SpriteBatch.Begin();
            for (int i = 0; i <= m_SiteItems.Count; i++)
            {
                m_SpriteBatch.Draw(separation, DrawPos + new Vector2(5, m_SiteItemsStartY + m_SiteItemHeight*i) - new Vector2(0, separation.Height / 2), Color.White);
            }
            //m_SpriteBatch.Draw(icon, DrawPos + new Vector2(50, 100) - new Vector2(icon.Width/2, icon.Height/2), Color.White);
            //m_SpriteBatch.Draw(frame, DrawPos + new Vector2(50, 100) - new Vector2(frame.Width/2, frame.Height/2), Color.White);
            //m_SpriteBatch.DrawString(font, "Menifestollia", DrawPos + new Vector2(91, 86), Color.Black);
            //m_SpriteBatch.DrawString(font, "Menifestollia", DrawPos + new Vector2(90, 85), Color.LightYellow);

            m_SpriteBatch.End();
        }

        protected Tile m_TileViewing;

        public Tile TileViewing
        {
            get { return m_TileViewing; }
            set
            {
                if (m_TileViewing != value)
                {
                    foreach (Window siteItem in m_SiteItems)
                        m_ChildWnds.Remove(siteItem);
                    m_SiteItems.Clear();

                    AddSiteItems(value.Sites, 0);

                    m_TileViewing = value;
                }
            }
        }

        protected List<Window> m_SiteItems = new List<Window>();
        protected int m_SiteItemsLayer;
        protected int m_SiteItemHeight = 48;
        protected int m_SiteItemIndent = 20;
        protected int m_SiteItemsStartY = 20;
        protected int m_SiteItemsStartX = 20;

        protected void AddSiteItems(SiteCollection sites, int indent)
        {
            foreach (Site site in sites)
            {
                Window itemWnd = site.CreateListItem(this);
                itemWnd.Layer = m_SiteItemsLayer;
                itemWnd.Layout.SetLeft(0, m_SiteItemsStartX + indent);
                itemWnd.Layout.SetTop(0, m_SiteItemsStartY + m_SiteItems.Count * m_SiteItemHeight);
                itemWnd.Layout.SetHeight(0, m_SiteItemHeight, false);
                m_SiteItems.Add(itemWnd);
                AddSiteItems(site.SubSites, indent + m_SiteItemIndent);
            }
        }
    }
}
